#!/bin/bash

set -euo pipefail
[ -n "${DEBUG:-}" ] && set -x
this="${BASH_SOURCE-$0}"
this_dir=$(cd -P -- "$(dirname -- "${this}")" && pwd -P)

WD=$(mktemp -d)

pushd "${this_dir}/.."

dev_tools/release "${WD}"
VERSION=$(<"VERSION")
pushd "${this_dir}"
cp "${WD}"/dist/pydoop-${VERSION}.tar.gz .
docker build -t crs4/pydoop-dist:latest -t crs4/pydoop-dist:${VERSION} -f Dockerfile.dist .
docker run --rm --name pydoop -p 8020:8020 -p 8042:8042 -p 8088:8088 -p 9000:9000 -p 10020:10020 -p 19888:19888 -p 50010:50010 -p 50020:50020 -p 50070:50070 -p 50075:50075 -p 50090:50090 -d crs4/pydoop-dist:${VERSION}
docker exec pydoop bash -c 'while : ; do jps | grep DataNode && break; done'
# smoke test
for v in 2 3; do
    docker exec pydoop bash -c "source /etc/profile && cd test && pydoop${v} -V"
    docker exec pydoop bash -c "source /etc/profile && cd test && python${v} -c 'import pydoop.version as v; assert v.version == \"${VERSION}\"; assert v.git_rev'"
    docker exec pydoop bash -c "source /etc/profile && cd examples/wordcount && bash run_wc.sh full"
done
docker stop pydoop

## Actual deployment disabled for now
# docker login -u "${DOCKER_USERNAME}" -p "${DOCKER_PASSWORD}"
# for img in pydoop pydoop-dist pydoop-docs; do
#     for tag in latest ${VERSION}; do
# 	docker push crs4/${img}:${tag}
#     done
# done

popd
popd
